package cn.devices.service.impl;

import cn.devices.dto.AndroidUserDto;
import cn.devices.dto.DmUserDto;
import cn.devices.entity.TbUser;
import cn.devices.mapper.TbUserMapper;
import cn.devices.model.UserModel;
import cn.devices.service.TbUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 员工表 服务实现类
 * </p>
 *
 * @author 秋水浮萍
 * @since 2021-02-07
 */
@Service
public class TbUserServiceImpl implements TbUserService {

    @Autowired
    private TbUserMapper userMapper;

    @Override
    public int tbUserAdd(TbUser user) {
        return userMapper.tbUserAdd(user);
    }

    @Override
    public int tbUserUpdate(TbUser user) {
        return userMapper.tbUserUpdate(user);
    }

    @Override
    public int tbUserDeleteFalse(String userId) {
        return userMapper.tbUserDeleteFalse(userId);
    }

    @Override
    public int tbUserDeleteTrue(String userId) {
        return userMapper.tbUserDeleteTrue(userId);
    }

    @Override
    public int countTel(String telephone, String userId) {
        return userMapper.countTel(telephone, userId);
    }

    @Override
    public TbUser userGetById(String userId) {
        return userMapper.userGetById(userId);
    }

    @Override
    public PageInfo<TbUser> tbUserList(int pageIndex, int pageSize, String depName) {
        PageHelper.startPage(pageIndex, pageSize);
        List<TbUser> list = userMapper.tbUserList(depName);
        for (TbUser user : list) {



            if ("1".equals(user.getUserStatus())) {
                user.setUserStatus("可借");
            } else {
                user.setUserStatus("不可借");
            }
        }
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<TbUser> dpUserList(int pageIndex, int pageSize, String depCode) {
        PageHelper.startPage(pageIndex, pageSize);
        List<TbUser> list = userMapper.dpUserList(depCode);
        return new PageInfo<>(list);
    }

    @Override
    public List<DmUserDto> dmUserDtoList() {
        List<DmUserDto> list = userMapper.dmUserDtos();
        List<UserModel> userModelList = userMapper.userModelList();
        for (DmUserDto dmUserDto : list
        ) {
            if (dmUserDto.getUserList() == null) dmUserDto.setUserList(new ArrayList<>());
            for (UserModel userModel : userModelList) {
                if (dmUserDto.getDepCode().equals(userModel.getDepCode())) {
                    dmUserDto.getUserList().add(userModel);
                }
            }
        }

        return list;
    }

    @Override
    public List<AndroidUserDto> androidUserDtos() {
        List<AndroidUserDto> dtos = userMapper.androidUserDtos();

        return dtos;
    }
}
